import { handleActions as createReducer } from "redux-actions";
import { addProductToLocalCart, changeLocalProductNumber, deleteProductFromLocalCart, saveCarts } from "../actions/cart.actions";
import productSaga from "../sagas/product.saga";

const initialState = [];

const handleAddProductToCart = (state, action) => {
  const newState = JSON.parse(JSON.stringify(state));
  const product = newState.find(product => product.id === action.payload.id);
  if (product) {
    product.count = product.count + 1;
  } else {
    newState.push(action.payload);
  }
  return newState;
}

const handleSaveCarts = (state, action) => action.payload;

const handleDeleteProductFromLocalCart = (state, action) => {
  const newState = JSON.parse(JSON.stringify(state));
  newState.splice(action.payload, 1);
  return newState;
}

const handleChangeLocalProductNumber = (state, action) => {
  const newState = JSON.parse(JSON.stringify(state));
  const product = newState.find(product => product.id === action.payload.id);
  product.count = action.payload.count;
  return newState;
}
export default createReducer({
  [addProductToLocalCart]: handleAddProductToCart,
  [saveCarts]: handleSaveCarts,
  [deleteProductFromLocalCart]: handleDeleteProductFromLocalCart,
  [changeLocalProductNumber]: handleChangeLocalProductNumber
}, initialState);